import axios from 'axios'
import { Loading } from 'element-ui'
import router from '@/router'
import { message } from './resetMessage'
const instance = axios.create({
  baseURL: 'http://120.24.171.137:1337/',
  timeout: 10000
})
// 声明加载接收返回的对象,后面用于关闭加载
let loading
// 请求拦截器
instance.interceptors.request.use(config => {
// Do something before request is sent
  // 加载
  loading = Loading.service({ text: '正在加载中', spinner: 'el-icon-loading', background: 'rgba(0, 0, 0, 0.8)' })
  return config
}, error => {
// Do something with request error
  loading.close()
  return Promise.reject(error)
})

// 响应拦截器
instance.interceptors.response.use(response => {
  console.log(response)
  if (router.path !== '/arceList') {
    if (response.status === 200) {
      message({
        type: 'success',
        message: response.statusText
      })
    }
  }
  // Do something before response is sent
  loading.close()
  return response
}, error => {
// Do something with response error
  if (error.response.data.statusCode === 400) {
    message({
      message: `${error.response.data.data[0].messages[0].id}`,
      // message: '用户名或者邮箱已被注册',
      type: 'error'
    })
  }
  // }
  loading.close()
  return Promise.reject(error)
})
export default instance
